package com.mlxg.config.mmapper;

import com.mlxg.master.domain.HzExhibitor;
import com.mlxg.master.domain.HzExhibitsReceive;
import com.mlxg.master.domain.HzProductSeries;
import com.mlxg.selver.domain.ExUser;
import com.mlxg.selver.domain.ExUserAttach;
import com.mlxg.selver.domain.HzExhibitsSlave;
import com.mlxg.selver.domain.HzProductSeriesSlave;
import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;
import org.modelmapper.TypeMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.IOException;

/**
 * @author cy zhang
 * @version 1.0
 * @date 2025/4/10 10:52
 */
@Component
public class InitMappingEntityCofing {
    @Autowired
    private ModelMapper modelMapper;

    @PostConstruct // 确保只执行一次
    private void configureAllMappings() {
        configureExUserMapping();
        configureExUserAttachMapping();
        configureHzProductSeriesMapping();
        configureHzExhibitsReceiveMapping();
    }

    /**
     * 展商信息映射
     */
    private void configureExUserMapping() {
        // 检查是否已存在映射
        if (modelMapper.getTypeMap(HzExhibitor.class, ExUser.class) == null) {
            modelMapper.createTypeMap(HzExhibitor.class, ExUser.class)
                    .addMapping(HzExhibitor::getRegistAccount, ExUser::setAccount)//注册账号
                    .addMapping(HzExhibitor::getUserName, ExUser::setUserName) //用户名
                    .addMapping(HzExhibitor::getSex, ExUser::setUserSex) //性别
                    .addMapping(HzExhibitor::getUserTel, ExUser::setUserPhone)//手机号
                    .addMapping(HzExhibitor::getEmail, ExUser::setUserEmail);//邮箱

        }
    }

    /**
     * 展商公司信息映射
     */
    private void configureExUserAttachMapping() {
        if (modelMapper.getTypeMap(HzExhibitor.class, ExUserAttach.class) == null) {
            modelMapper.createTypeMap(HzExhibitor.class, ExUserAttach.class)
                    .addMapping(HzExhibitor::getCompanyNameC, ExUserAttach::setCompanyNameC)
                    .addMapping(HzExhibitor::getCompanyNameE, ExUserAttach::setCompanyNameE)
                    .addMapping(HzExhibitor::getEnteProperty, ExUserAttach::setEnterpriseNature) //企业性质
                    .addMapping(HzExhibitor::getUnitAbbreviation, ExUserAttach::setEnterpriseAbbreviation) //企业简称
                    .addMapping(HzExhibitor::getAbstractC, ExUserAttach::setCompanyAddress) //公司地址
                    .addMapping(HzExhibitor::getPostCode, ExUserAttach::setZipCode)//邮编
                    .addMapping(HzExhibitor::getTelephone, ExUserAttach::setCompanyPhone)
                    .addMapping(HzExhibitor::getComUrl, ExUserAttach::setCompanyWebsite)//网址
                    .addMapping(HzExhibitor::getComCover, ExUserAttach::setEnterpriseLogo) //公司logo中文
                    .addMapping(HzExhibitor::getRemarks, ExUserAttach::setEnterpriseLogoEn) //公司logo英文
                    .addMapping(HzExhibitor::getColumn16, ExUserAttach::setCompanyWechatPublicNumberUrl) //微信二维码
                    .addMapping(HzExhibitor::getColumn17, ExUserAttach::setCompanyIntroduceCn) //公司介绍中文
                    .addMapping(HzExhibitor::getColumn18, ExUserAttach::setCompanyIntroduceEn) //公司介绍引文
                    .addMapping(HzExhibitor::getColumn23, ExUserAttach::setEnterprisePublicityVideoUrl) //公司宣传视频封面地址
                    .addMapping(HzExhibitor::getColumn25, ExUserAttach::setVideoUrl); // 公司宣传视频地址
        }
    }

    /**
     * 自定义产品维护
     */
    private void configureHzProductSeriesMapping() {
        if (modelMapper.getTypeMap(HzProductSeries.class, HzProductSeriesSlave.class) == null) {
            modelMapper.createTypeMap(HzProductSeries.class, HzProductSeriesSlave.class)
                    .addMapping(HzProductSeries::getName, HzProductSeriesSlave::setName)
                    .addMapping(HzProductSeries::getSort, HzProductSeriesSlave::setSort)
                    .addMappings(mapper -> mapper.skip(HzProductSeriesSlave::setId)); // 显式跳过id字段
        }
    }

    /**
     * 展商展品数据映射
     */
    private void configureHzExhibitsReceiveMapping() {
        if (modelMapper.getTypeMap(HzExhibitsReceive.class, HzExhibitsSlave.class) == null) {
            modelMapper.createTypeMap(HzExhibitsReceive.class, HzExhibitsSlave.class)
                    .addMapping(HzExhibitsReceive::getExhibitsName, HzExhibitsSlave::setProductC)//  - 展品名称·中文
                    .addMapping(HzExhibitsReceive::getColumn25, HzExhibitsSlave::setProductE)  // -   - 展品名称·英文
                    .addMapping(HzExhibitsReceive::getColumn23, HzExhibitsSlave::setProductModel)//  -   - 展品型号
//                    .addMapping(HzExhibitsReceive::getColumn19, HzExhibitsSlave::setProductTypeId) //展品类型
//                    .addMapping(HzExhibitsReceive::getApplyTime, HzExhibitsSlave::setApplyTime) //申请时间
                    .addMapping(HzExhibitsReceive::getColumn11, HzExhibitsSlave::setIsRecommend) //是否推荐
//                    .addMapping(HzExhibitsReceive::getColumn15, HzExhibitsSlave::setProductCustomizeType) //  - 展品自定义类型
//                    .addMapping(HzExhibitsReceive::getExhibitsImg, HzExhibitsSlave::setProductPictureUrl) //展品图片
//                    .addMapping(HzExhibitsReceive::getExhibitsAttach, HzExhibitsSlave::setProductInformationUrl) //展品资料
                    .addMapping(HzExhibitsReceive::getExhibitsIntro, HzExhibitsSlave::setProductIntroduceC)//展品介绍中文
                    .addMapping(HzExhibitsReceive::getExhibitsIntroE, HzExhibitsSlave::setProductIntroduceE)//展品·介绍英文
                    .addMapping(HzExhibitsReceive::getTechParam, HzExhibitsSlave::setProductParameterC) //展品参数中文
                    .addMapping(HzExhibitsReceive::getColumn24, HzExhibitsSlave::setProductParameterE) //展品参数英文
//                    .addMapping(HzExhibitsReceive::getColumn20, HzExhibitsSlave::setProductVideoImageUrl)//  - 展品宣传视频封面
//                    .addMapping(HzExhibitsReceive::getColumn21, HzExhibitsSlave::setProductVideoUrl)// 产品视频
                    .addMappings(mapper -> mapper.skip(HzExhibitsSlave::setId)); // 显式跳过id字段
        }
    }
}
